package com.example.asperm

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.annotation.NonNull


import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
import io.flutter.plugin.common.MethodChannel
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.withContext
//import kotlinx.coroutines.launch

class MainActivity: FlutterActivity(){

    private var mContext: Context? = null
    private var myFlutterPlugins: MyFlutterPlugins? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("TAG","MainActivity -- onCreate")
//        mContext = this
    }

//    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
//        Log.e("TAG","onAttachedToEngine111")
//
//        super.configureFlutterEngine(flutterEngine)
//        if(myFlutterPlugins == null){
//            myFlutterPlugins = MyFlutterPlugins(this);
//        }
//        flutterEngine.plugins.add(myFlutterPlugins!!)
//    }




//    override fun onRequestPermissionsResult(
//        requestCode: Int,
//        permissions: Array<out String>,
//        grantResults: IntArray
//    ) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//    }
//
//    protected fun onPermissionsOk() {
//    }
//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//    }

}


class MyFlutterPlugins (val context: Context) : FlutterPlugin{

    init {
        Log.e("TAG","----init")
    }

    //FlutterPlugin与FlutterEngine实例关联  让flutter层与android原生的交互进行绑定
    override fun onAttachedToEngine(binding: FlutterPluginBinding) {
        //获取binaryMessenger对象
        val binaryMessenger=binding.binaryMessenger
        //创建methodChannel对象
        val methodChannel=MethodChannel(binaryMessenger, "my_channel")

//        var analysisResult by remember { mutableStateOf<String?>(null) }


        //使用setMethodHandle对对方调用自己的方法进行监听，
        // 通过回调中的MethodCall对象方法名判断、获取方法参数，并且返回调用结果。
        Log.e("TAG","onAttachedToEngine222")


        methodChannel.setMethodCallHandler { call, result ->
            if (call.method == "videoPath") {
                Log.e("TAG","----获取到 videoPat:${call.arguments}")
                val videoPath = call.arguments as String

//                analysisResult = null


                if (videoPath != null) {


//                    val result = kotlinx.coroutines.withContext(Dispatchers.IO) {
//                        val config = Configuration()
//                        val motility = Motility(context, config)
//                        val result = motility.analyze(videoPath)
////                    }
//                    Log.d("Analysis", "Result: ${result?.toString()}")
                } else {

                    Log.e("VideoProcessing", "Could not get file path from URI")
                }


            }

        }


    }

    override fun onDetachedFromEngine(binding: FlutterPluginBinding) {}


}